`
https://leetcode.cn/problems/find-mode-in-binary-search-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var findMode = function (root) {
  let res = []
  let curCount = 0, maxCount = 0
  let lastNode = null

  const traverse = (root) => {
    if (root === null) return

    traverse(root.left)

    if (!lastNode) {
      // 开始遍历时，初始化
      curCount = 1
      maxCount = 1
      res.push(root.val)
    } else {
      // 如果当前值和上个值相同
      if (lastNode.val === root.val) {
        // 累加到结果上
        curCount++
        if (curCount === maxCount) {
          res.push(root.val)
        }
        // 出现新的众数
        else if (curCount > maxCount) {
          res = [root.val]
          maxCount = curCount
        }
      }
      // 不相同则重新计数
      else {
        curCount = 1
        if (curCount === maxCount) {
          res.push(root.val)
        }
      }
    }
    lastNode = root

    traverse(root.right)
  }

  traverse(root)

  return res
};